home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 26 / AMIGAplus Sonderheft 26 (2000)(Falke)(DE)(Track 1 of 2)[!].iso / Tools / GFX-Viewer / Animviewer / mpegvideo_datatype / mpeg16bit.c < prev    next >
C/C++ Source or Header  |  1999-03-29  |  21KB  |  811 lines

  1.  
  2. /*
  3. **
  4. **  $VER: mpeg16bit.c 1.11 (2.11.97)
  5. **  mpegvideo.datatype 1.10
  6. **
  7. **  16 bit dither routines, got from mpeg_play 2.3
  8. **
  9. **  Written 1997 by Roland 'Gizzy' Mainz
  10. **
  11. */
  12.  
  13. /* project includes */
  14. #include "mpegvideo.h"
  15. #include "mpegproto.h"
  16.  
  17. /* ansi includes */
  18. #include <math.h>
  19.  
  20. /*
  21. #define INTERPOLATE 1
  22. */
  23.  
  24. #define GAMMA_CORRECTION(x) ((int)(pow((x) / 255.0, 1.0 / gammaCorrect) * 255.0))
  25. #define CHROMA_CORRECTION256(x) ((x) >= 128 \
  26.                         ? 128 + min(127, (int)(((x) - 128.0) * chromaCorrect)) \
  27.                         : 128 - min(128, (int)((128.0 - (x)) * chromaCorrect)))
  28. #define CHROMA_CORRECTION128(x) ((x) >= 0 \
  29.                         ? min(127,  (int)(((x) * chromaCorrect))) \
  30.                         : max(-128, (int)(((x) * chromaCorrect))))
  31. #define CHROMA_CORRECTION256D(x) ((x) >= 128 \
  32.                         ? 128.0 + min(127.0, (((x) - 128.0) * chromaCorrect)) \
  33.                         : 128.0 - min(128.0, (((128.0 - (x)) * chromaCorrect))))
  34. #define CHROMA_CORRECTION128D(x) ((x) >= 0 \
  35.                         ? min(127.0,  ((x) * chromaCorrect)) \
  36.                         : max(-128.0, ((x) * chromaCorrect)))
  37.  
  38.  
  39. /*
  40.  * Erik Corry's multi-byte dither routines.
  41.  *
  42.  * The basic idea is that the Init generates all the necessary tables.
  43.  * The tables incorporate the information about the layout of pixels
  44.  * in the XImage, so that it should be able to cope with 15-bit, 16-bit
  45.  * 24-bit (non-packed) and 32-bit (10-11 bits per color!) screens.
  46.  * At present it cannot cope with 24-bit packed mode, since this involves
  47.  * getting down to byte level again. It is assumed that the bits for each
  48.  * color are contiguous in the longword.
  49.  *
  50.  * Writing to memory is done in shorts or ints. (Unfortunately, short is not
  51.  * very fast on Alpha, so there is room for improvement here). There is no
  52.  * dither time check for overflow - instead the tables have slack at
  53.  * each end. This is likely to be faster than an 'if' test as many modern
  54.  * architectures are really bad at ifs. Potentially, each '&&' causes a 
  55.  * pipeline flush!
  56.  *
  57.  * There is no shifting and fixed point arithmetic, as I really doubt you
  58.  * can see the difference, and it costs. This may be just my bias, since I
  59.  * heard that Intel is really bad at shifting.
  60.  */
  61.  
  62. /*
  63.  * How many 1 bits are there in the longword.
  64.  * Low performance, do not call often.
  65.  */
  66. static
  67. int number_of_bits_set( unsigned long a )
  68. {
  69.     if( !a )
  70.     {
  71.       return( 0 );
  72.     }
  73.  
  74.     if( a & 1 )
  75.       return( (1 + number_of_bits_set( (a >> 1) )) );
  76.  
  77.     return( number_of_bits_set( (a >> 1) ) );
  78. }
  79.  
  80.  
  81. /*
  82.  * How many 0 bits are there at least significant end of longword.
  83.  * Low performance, do not call often.
  84.  */
  85. static
  86. int free_bits_at_bottom( unsigned long a )
  87. {
  88.     /* assume char is 8 bits */
  89.     if( !a )
  90.     {
  91.       return( sizeof( unsigned long ) * 8 );
  92.     }
  93.  
  94.     if( ((long)a) & 1L )
  95.     {
  96.       return( 0 );
  97.     }
  98.  
  99.     return( (1 + free_bits_at_bottom ( (a >> 1) )) );
  100. }
  101.  
  102.  
  103. /*
  104.  *--------------------------------------------------------------
  105.  *
  106.  * InitColor16Dither --
  107.  *
  108.  *    To get rid of the multiply and other conversions in color
  109.  *    dither, we use a lookup table.
  110.  *
  111.  * Results:
  112.  *    None.
  113.  *
  114.  * Side effects:
  115.  *    The lookup tables are initialized.
  116.  *
  117.  *--------------------------------------------------------------
  118.  */
  119.  
  120. void InitColorDither( struct MPEGVideoInstData *mvid )
  121. {
  122.     unsigned long red_mask;
  123.     unsigned long green_mask;
  124.     unsigned long blue_mask;
  125.     BOOL          thirty2;
  126.     BOOL          gammaCorrectFlag  = (gammaCorrect  != 1.0);
  127.     BOOL          chromaCorrectFlag = (chromaCorrect != 1.0);
  128.     int           CR,
  129.                   CB,
  130.                   i;
  131.  
  132.     /* set up pixel masks */
  133.     switch( anim_depth )
  134.     {
  135.       case 32: /* XRGB, 8R, 8G, 8B */
  136.       case 24: /* RGB, 8R, 8G, 8B */
  137.       case 8:  /* HAM-8 */
  138.       case 6:  /* HAM-6 */
  139.       {
  140.           thirty2 = TRUE;
  141.           red_mask   = 0x000000FFUL;
  142.           green_mask = 0x0000FF00UL;
  143.           blue_mask  = 0x00FF0000UL;
  144.       }
  145.           break;
  146.  
  147.       case 16: /* 5R, 5G, 5B */
  148.       {
  149.           thirty2 = FALSE;
  150.           red_mask   = 0x001FUL;
  151.           green_mask = 0x07E0UL;
  152.           blue_mask  = 0xF800UL;
  153.       }
  154.           break;
  155.  
  156.       default:
  157.       {
  158.           thirty2 = FALSE;
  159.           red_mask   = 0UL;
  160.           green_mask = 0UL;
  161.           blue_mask  = 0UL;
  162.           
  163.           error_printf( mvid, "unsupported chunkypixel depth\n" );
  164.       }
  165.           break;
  166.     }
  167.  
  168.     if( L_tab == NULL )          L_tab          = (long *)mymalloc( mvid, 256 * sizeof( long ) );
  169.     if( Cr_r_tab == NULL )       Cr_r_tab       = (long *)mymalloc( mvid, 256 * sizeof( long ) );
  170.     if( Cr_g_tab == NULL )       Cr_g_tab       = (long *)mymalloc( mvid, 256 * sizeof( long ) );
  171.     if( Cb_g_tab == NULL )       Cb_g_tab       = (long *)mymalloc( mvid, 256 * sizeof( long ) );
  172.     if( Cb_b_tab == NULL )       Cb_b_tab       = (long *)mymalloc( mvid, 256 * sizeof( long ) );
  173.     if( r_2_pix_alloc == NULL )  r_2_pix_alloc  = (long *)mymalloc( mvid, 768 * sizeof( long ) );
  174.     if( g_2_pix_alloc == NULL )  g_2_pix_alloc  = (long *)mymalloc( mvid, 768 * sizeof( long ) );
  175.     if( b_2_pix_alloc == NULL )  b_2_pix_alloc  = (long *)mymalloc( mvid, 768 * sizeof( long ) );
  176.  
  177.     for( i = 0 ; i < 256 ; i++ )
  178.     {
  179.       L_tab[ i ] = i;
  180.  
  181.       if( gammaCorrectFlag )
  182.       {
  183.         L_tab[ i ] = GAMMA_CORRECTION( i );
  184.       }
  185.  
  186.       CB = CR = i;
  187.  
  188.       if( chromaCorrectFlag )
  189.       {
  190.         CB -= 128;
  191.         CB = CHROMA_CORRECTION128( CB );
  192.         CR -= 128;
  193.         CR = CHROMA_CORRECTION128( CR );
  194.       }
  195.       else
  196.       {
  197.         CB -= 128; CR -= 128;
  198.       }
  199. /* was
  200.  *    Cr_r_tab[i] =  1.596 * CR;
  201.  *    Cr_g_tab[i] = -0.813 * CR;
  202.  *    Cb_g_tab[i] = -0.391 * CB;
  203.  *    Cb_b_tab[i] =  2.018 * CB;
  204.  *  but they were just messed up.
  205.  *  Then was (_Video Deymstified_):
  206.  *    Cr_r_tab[i] =  1.366 * CR;
  207.  *    Cr_g_tab[i] = -0.700 * CR;
  208.  *    Cb_g_tab[i] = -0.334 * CB;
  209.  *    Cb_b_tab[i] =  1.732 * CB;
  210.  *  but really should be:
  211.  *   (from ITU-R BT.470-2 System B, G and SMPTE 170M )
  212.  */
  213.       Cr_r_tab[ i ] =  (0.419 / 0.299) * CR;
  214.       Cr_g_tab[ i ] = -(0.299 / 0.419) * CR;
  215.       Cb_g_tab[ i ] = -(0.114 / 0.331) * CB;
  216.       Cb_b_tab[ i ] =  (0.587 / 0.331) * CB;
  217.  
  218. /*
  219.  *  though you could argue for:
  220.  *    SMPTE 240M
  221.  *      Cr_r_tab[i] =  (0.445/0.212) * CR;
  222.  *      Cr_g_tab[i] = -(0.212/0.445) * CR;
  223.  *      Cb_g_tab[i] = -(0.087/0.384) * CB;
  224.  *      Cb_b_tab[i] =  (0.701/0.384) * CB;
  225.  *    FCC
  226.  *      Cr_r_tab[i] =  (0.421/0.30) * CR;
  227.  *      Cr_g_tab[i] = -(0.30/0.421) * CR;
  228.  *      Cb_g_tab[i] = -(0.11/0.331) * CB;
  229.  *      Cb_b_tab[i] =  (0.59/0.331) * CB;
  230.  *    ITU-R BT.709
  231.  *      Cr_r_tab[i] =  (0.454/0.2125) * CR;
  232.  *      Cr_g_tab[i] = -(0.2125/0.454) * CR;
  233.  *      Cb_g_tab[i] = -(0.0721/0.386) * CB;
  234.  *      Cb_b_tab[i] =  (0.7154/0.386) * CB;
  235.  *
  236.  */
  237.     }
  238.  
  239.     /*
  240.      * Set up entries 0-255 in rgb-to-pixel value tables.
  241.      */
  242.     for( i = 0 ; i < 256 ; i++ )
  243.     {
  244.       r_2_pix_alloc[ i + 256 ]   = i >> (8 - number_of_bits_set( red_mask ));
  245.       r_2_pix_alloc[ i + 256 ] <<= free_bits_at_bottom( red_mask );
  246.       g_2_pix_alloc[ i + 256 ]   = i >> (8 - number_of_bits_set( green_mask ));
  247.       g_2_pix_alloc[ i + 256 ] <<= free_bits_at_bottom( green_mask );
  248.       b_2_pix_alloc[ i + 256 ]   = i >> (8 - number_of_bits_set( blue_mask ));
  249.       b_2_pix_alloc[ i + 256 ] <<= free_bits_at_bottom( blue_mask );
  250.       /*
  251.        * If we have 16-bit output depth, then we double the value
  252.        * in the top word. This means that we can write out both
  253.        * pixels in the pixel doubling mode with one op. It is
  254.        * harmless in the normal case as storing a 32-bit value
  255.        * through a short pointer will lose the top bits anyway.
  256.        * A similar optimisation for Alpha for 64 bit has been
  257.        * prepared for, but is not yet implemented.
  258.        */
  259.       if( !thirty2 )
  260.       {
  261.         r_2_pix_alloc[ i + 256 ] |= (r_2_pix_alloc[ i + 256 ]) << 16;
  262.         g_2_pix_alloc[ i + 256 ] |= (g_2_pix_alloc[ i + 256 ]) << 16;
  263.         b_2_pix_alloc[ i + 256 ] |= (b_2_pix_alloc[ i + 256 ]) << 16;
  264.       }
  265.     }
  266.  
  267.     /*
  268.      * Spread out the values we have to the rest of the array so that
  269.      * we do not need to check for overflow.
  270.      */
  271.     for( i = 0 ; i < 256 ; i++ )
  272.     {
  273.       r_2_pix_alloc[ i       ] = r_2_pix_alloc[ 256 ];
  274.       r_2_pix_alloc[ i + 512 ] = r_2_pix_alloc[ 511 ];
  275.       g_2_pix_alloc[ i       ] = g_2_pix_alloc[ 256 ];
  276.       g_2_pix_alloc[ i + 512 ] = g_2_pix_alloc[ 511 ];
  277.       b_2_pix_alloc[ i       ] = b_2_pix_alloc[ 256 ];
  278.       b_2_pix_alloc[ i + 512 ] = b_2_pix_alloc[ 511 ];
  279.     }
  280.  
  281.     r_2_pix = r_2_pix_alloc + 256;
  282.     g_2_pix = g_2_pix_alloc + 256;
  283.     b_2_pix = b_2_pix_alloc + 256;
  284. }
  285.  
  286.  
  287. /*
  288.  *--------------------------------------------------------------
  289.  *
  290.  * Color16DitherImage --
  291.  *
  292.  *    Converts image into 16 bit color.
  293.  *
  294.  * Results:
  295.  *    None.
  296.  *
  297.  * Side effects:
  298.  *    None.
  299.  *
  300.  *--------------------------------------------------------------
  301.  */
  302.  
  303. void Color16DitherImage( struct MPEGVideoInstData *mvid, UBYTE *lum, UBYTE *cr, UBYTE *cb, UBYTE *out, UWORD cols, UWORD rows )
  304. {
  305.     int             L, 
  306.                     CR, 
  307.                     CB;
  308.     unsigned short *row1,
  309.                    *row2;
  310.     unsigned char  *lum2;
  311.     UWORD           x,
  312.                     y;
  313.     int             cr_r;
  314.     int             cr_g;
  315.     int             cb_g;
  316.     int             cb_b;
  317.     int             cols_2 = cols/2;
  318.  
  319.     row1 = (unsigned short *)out;
  320.     row2 = row1 + cols_2 + cols_2;
  321.     lum2 = lum  + cols_2 + cols_2;
  322.  
  323.     for( y = 0 ; y < rows ; y += 2 )
  324.     {
  325.       for( x = 0 ; x < cols_2 ; x++ )
  326.       {
  327.         int R, G, B;
  328.  
  329.         CR = *cr++;
  330.         CB = *cb++;
  331.         cr_r = Cr_r_tab[CR];
  332.         cr_g = Cr_g_tab[CR];
  333.         cb_g = Cb_g_tab[CB];
  334.         cb_b = Cb_b_tab[CB];
  335.  
  336.             L = L_tab[(int) *lum++];
  337.  
  338.         R = L + cr_r;
  339.         G = L + cr_g + cb_g;
  340.         B = L + cb_b;
  341.  
  342.         *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  343.  
  344. #ifdef INTERPOLATE
  345.         if(x != cols_2 - 1)
  346.         {
  347.           CR = (CR + *cr) >> 1;
  348.           CB = (CB + *cb) >> 1;
  349.           cr_r = Cr_r_tab[CR];
  350.           cr_g = Cr_g_tab[CR];
  351.           cb_g = Cb_g_tab[CB];
  352.           cb_b = Cb_b_tab[CB];
  353.         }
  354. #endif
  355.  
  356.             L = L_tab[(int) *lum++];
  357.  
  358.         R = L + cr_r;
  359.         G = L + cr_g + cb_g;
  360.         B = L + cb_b;
  361.  
  362.         *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  363.  
  364.         /*
  365.          * Now, do second row.
  366.          */
  367. #ifdef INTERPOLATE
  368.         if(y != rows - 2) 
  369.         {
  370.           CR = (CR + *(cr + cols_2 - 1)) >> 1;
  371.           CB = (CB + *(cb + cols_2 - 1)) >> 1;
  372.           cr_r = Cr_r_tab[CR];
  373.           cr_g = Cr_g_tab[CR];
  374.           cb_g = Cb_g_tab[CB];
  375.           cb_b = Cb_b_tab[CB];
  376.         }
  377. #endif
  378.  
  379.         L = L_tab[(int) *lum2++];
  380.         R = L + cr_r;
  381.         G = L + cr_g + cb_g;
  382.         B = L + cb_b;
  383.  
  384.         *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  385.  
  386.         L = L_tab[(int) *lum2++];
  387.         R = L + cr_r;
  388.         G = L + cr_g + cb_g;
  389.         B = L + cb_b;
  390.  
  391.         *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  392.       }
  393.  
  394.       /*
  395.        * These values are at the start of the next line, (due
  396.        * to the ++'s above),but they need to be at the start
  397.        * of the line after that.
  398.        */
  399.       lum += cols_2 + cols_2;
  400.       lum2 += cols_2 + cols_2;
  401.       row1 += cols_2 + cols_2;
  402.       row2 += cols_2 + cols_2;
  403.     }
  404. }
  405.  
  406.  
  407. /*
  408.  *--------------------------------------------------------------
  409.  *
  410.  * Color32DitherImage --
  411.  *
  412.  *    Converts image into 32 bit color (or 24-bit non-packed).
  413.  *
  414.  * Results:
  415.  *    None.
  416.  *
  417.  * Side effects:
  418.  *    None.
  419.  *
  420.  *--------------------------------------------------------------
  421.  */
  422.  
  423. /*
  424.  * This is a copysoft version of the function above with ints instead
  425.  * of shorts to cause a 4-byte pixel size
  426.  */
  427.  
  428. void Color32DitherImage( struct MPEGVideoInstData *mvid, UBYTE *lum, UBYTE *cr, UBYTE *cb, UBYTE *out, UWORD cols, UWORD rows )
  429. {
  430.     int            L, CR, CB;
  431.     unsigned int  *row1,
  432.                   *row2;
  433.     unsigned char *lum2;
  434.     UWORD          x, 
  435.                    y;
  436.     int            cr_r;
  437.     int            cr_g;
  438.     int            cb_g;
  439.     int            cb_b;
  440.     int            cols_2 = cols / 2;
  441.  
  442.     row1 = (unsigned int *)out;
  443.     row2 = row1 + cols_2 + cols_2;
  444.     lum2 = lum + cols_2 + cols_2;
  445.  
  446.     for( y = 0 ; y < rows ; y += 2 )
  447.     {
  448.       for( x = 0 ; x < cols_2 ; x++ )
  449.       {
  450.         int R, G, B;
  451.  
  452.         CR = *cr++;
  453.         CB = *cb++;
  454.         cr_r = Cr_r_tab[CR];
  455.         cr_g = Cr_g_tab[CR];
  456.         cb_g = Cb_g_tab[CB];
  457.         cb_b = Cb_b_tab[CB];
  458.  
  459.             L = L_tab[(int) *lum++];
  460.  
  461.         R = L + cr_r;
  462.         G = L + cr_g + cb_g;
  463.         B = L + cb_b;
  464.  
  465.         *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  466.  
  467. #ifdef INTERPOLATE
  468.         if(x != cols_2 - 1)
  469.         {
  470.           CR = (CR + *cr) >> 1;
  471.           CB = (CB + *cb) >> 1;
  472.           cr_r = Cr_r_tab[CR];
  473.           cr_g = Cr_g_tab[CR];
  474.           cb_g = Cb_g_tab[CB];
  475.           cb_b = Cb_b_tab[CB];
  476.         }
  477. #endif
  478.  
  479.             L = L_tab[(int) *lum++];
  480.  
  481.         R = L + cr_r;
  482.         G = L + cr_g + cb_g;
  483.         B = L + cb_b;
  484.  
  485.         *row1++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  486.  
  487.         /*
  488.          * Now, do second row.
  489.          */
  490.  
  491. #ifdef INTERPOLATE
  492.         if(y != rows - 2) 
  493.         {
  494.           CR = (CR + *(cr + cols_2 - 1)) >> 1;
  495.           CB = (CB + *(cb + cols_2 - 1)) >> 1;
  496.           cr_r = Cr_r_tab[CR];
  497.           cr_g = Cr_g_tab[CR];
  498.           cb_g = Cb_g_tab[CB];
  499.           cb_b = Cb_b_tab[CB];
  500.         }
  501. #endif
  502.  
  503.         L = L_tab [(int) *lum2++];
  504.         R = L + cr_r;
  505.         G = L + cr_g + cb_g;
  506.         B = L + cb_b;
  507.  
  508.         *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  509.  
  510.         L = L_tab [(int) *lum2++];
  511.         R = L + cr_r;
  512.         G = L + cr_g + cb_g;
  513.         B = L + cb_b;
  514.  
  515.         *row2++ = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  516.       }
  517.  
  518.       lum  += cols_2 + cols_2;
  519.       lum2 += cols_2 + cols_2;
  520.       row1 += cols_2 + cols_2;
  521.       row2 += cols_2 + cols_2;
  522.     }
  523. }
  524.  
  525.  
  526. #ifdef COMMENTED_OUT
  527. /*
  528.  * Erik Corry's pixel doubling routines for 15/16/24/32 bit screens.
  529.  */
  530.  
  531.  
  532. /*
  533.  *--------------------------------------------------------------
  534.  *
  535.  * Twox2Color16DitherImage --
  536.  *
  537.  *    Converts image into 16 bit color at double size.
  538.  *
  539.  * Results:
  540.  *    None.
  541.  *
  542.  * Side effects:
  543.  *    None.
  544.  *
  545.  *--------------------------------------------------------------
  546.  */
  547.  
  548. /*
  549.  * In this function I make use of a nasty trick. The tables have the lower
  550.  * 16 bits replicated in the upper 16. This means I can write ints and get
  551.  * the horisontal doubling for free (almost).
  552.  */
  553.  
  554. void Twox2Color16DitherImage( struct MPEGVideoInstData *mvid, UBYTE *lum, UBYTE *cr, UBYTE *cb, UBYTE *out, UWORD cols, UWORD rows )
  555. {
  556.     int            L,
  557.                    CR,
  558.                    CB;
  559.     unsigned int  *row1 = (unsigned int *)out;
  560.     unsigned int  *row2 = row1 + cols;
  561.     unsigned int  *row3 = row2 + cols;
  562.     unsigned int  *row4 = row3 + cols;
  563.     unsigned char *lum2;
  564.     UWORD          x,
  565.                    y;
  566.     int            cr_r;
  567.     int            cr_g;
  568.     int            cb_g;
  569.     int            cb_b;
  570.     int            cols_2 = cols/2;
  571.  
  572.     lum2 = lum + cols_2 + cols_2;
  573.  
  574.     for( y = 0 ; y < rows ; y += 2 )
  575.     {
  576.       for( x = 0 ; x < cols_2 ; x++ )
  577.       {
  578.         int R, G, B;
  579.         int t;
  580.  
  581.         CR = *cr++;
  582.         CB = *cb++;
  583.         cr_r = Cr_r_tab[CR];
  584.         cr_g = Cr_g_tab[CR];
  585.         cb_g = Cb_g_tab[CB];
  586.         cb_b = Cb_b_tab[CB];
  587.  
  588.             L = L_tab[(int) *lum++];
  589.  
  590.         R = L + cr_r;
  591.         G = L + cr_g + cb_g;
  592.         B = L + cb_b;
  593.  
  594.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  595.         row1[0] = t;
  596.         row1++;
  597.         row2[0] = t;
  598.         row2++;
  599.  
  600. #ifdef INTERPOLATE
  601.         if( x != cols_2 - 1 )
  602.         {
  603.           CR = (CR + *cr) >> 1;
  604.           CB = (CB + *cb) >> 1;
  605.           cr_r = Cr_r_tab[CR];
  606.           cr_g = Cr_g_tab[CR];
  607.           cb_g = Cb_g_tab[CB];
  608.           cb_b = Cb_b_tab[CB];
  609.         }
  610. #endif
  611.             L = L_tab[(int) *lum++];
  612.  
  613.         R = L + cr_r;
  614.         G = L + cr_g + cb_g;
  615.         B = L + cb_b;
  616.  
  617.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  618.         row1[0] = t;
  619.         row1++;
  620.         row2[0] = t;
  621.         row2++;
  622.  
  623.         /*
  624.          * Now, do second row.
  625.          */
  626. #ifdef INTERPOLATE
  627.         if(y != rows - 2) 
  628.         {
  629.           CR = (CR + *(cr + cols_2 - 1)) >> 1;
  630.           CB = (CB + *(cb + cols_2 - 1)) >> 1;
  631.           cr_r = Cr_r_tab[CR];
  632.           cr_g = Cr_g_tab[CR];
  633.           cb_g = Cb_g_tab[CB];
  634.           cb_b = Cb_b_tab[CB];
  635.         }
  636. #endif
  637.         L = L_tab[(int) *lum2++];
  638.         R = L + cr_r;
  639.         G = L + cr_g + cb_g;
  640.         B = L + cb_b;
  641.  
  642.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  643.         row3[0] = t;
  644.         row3++;
  645.         row4[0] = t;
  646.         row4++;
  647.  
  648.         L = L_tab[(int) *lum2++];
  649.         R = L + cr_r;
  650.         G = L + cr_g + cb_g;
  651.         B = L + cb_b;
  652.  
  653.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  654.         row3[0] = t;
  655.         row3++;
  656.         row4[0] = t;
  657.         row4++;
  658.       }
  659.  
  660.       lum += cols_2 + cols_2;
  661.       lum2 += cols_2 + cols_2;
  662.       row1 += 6 * cols_2;
  663.       row3 += 6 * cols_2;
  664.       row2 += 6 * cols_2;
  665.       row4 += 6 * cols_2;
  666.     }
  667. }
  668.  
  669.  
  670. /*
  671.  *--------------------------------------------------------------
  672.  *
  673.  * Twox2Color32 --
  674.  *
  675.  *    Converts image into 24/32 bit color.
  676.  *
  677.  * Results:
  678.  *    None.
  679.  *
  680.  * Side effects:
  681.  *    None.
  682.  *
  683.  *--------------------------------------------------------------
  684.  */
  685.  
  686. #define ONE_TWO 2 /* used to skip positions on 32/64-bit architectures */
  687.  
  688. void Twox2Color32DitherImage( struct MPEGVideoInstData *mvid, UBYTE *lum, UBYTE *cr, UBYTE *cb, UBYTE *out, UWORD cols, UWORD rows )
  689. {
  690.     int            L, 
  691.                    CR, 
  692.                    CB;
  693.     unsigned long *row1 = (unsigned long *)out;
  694.     unsigned long *row2 = row1 + cols * ONE_TWO;
  695.     unsigned long *row3 = row2 + cols * ONE_TWO;
  696.     unsigned long *row4 = row3 + cols * ONE_TWO;
  697.     unsigned char *lum2;
  698.     UWORD          x, 
  699.                    y;
  700.     int            cr_r;
  701.     int            cr_g;
  702.     int            cb_g;
  703.     int            cb_b;
  704.     int            cols_2 = cols/2;
  705.  
  706.     lum2 = lum + cols_2 + cols_2;
  707.  
  708.     for( y = 0 ; y < rows ; y += 2 )
  709.     {
  710.       for( x = 0 ; x < cols_2 ; x++ )
  711.       {
  712.         int R, G, B;
  713.             long t;
  714.  
  715.         CR = *cr++;
  716.         CB = *cb++;
  717.         cr_r = Cr_r_tab[CR];
  718.         cr_g = Cr_g_tab[CR];
  719.         cb_g = Cb_g_tab[CB];
  720.         cb_b = Cb_b_tab[CB];
  721.  
  722.             L = L_tab[ (int) *lum++];
  723.  
  724.         R = L + cr_r;
  725.         G = L + cr_g + cb_g;
  726.         B = L + cb_b;
  727.  
  728.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  729.         row1[0] = t;
  730.         row2[0] = t;
  731.         row1 += ONE_TWO;
  732.         row2 += ONE_TWO;
  733.  
  734. /* INTERPOLATE is now standard */
  735. #ifdef INTERPOLATE
  736.         if(x != cols_2 - 1) 
  737.         {
  738.           CR = (CR + *cr) >> 1;
  739.           CB = (CB + *cb) >> 1;
  740.           cr_r = Cr_r_tab[CR];
  741.           cr_g = Cr_g_tab[CR];
  742.           cb_g = Cb_g_tab[CB];
  743.           cb_b = Cb_b_tab[CB];
  744.         }
  745. #endif
  746. /* end INTERPOLATE */
  747.             L = L_tab[ (int) *lum++];
  748.  
  749.         R = L + cr_r;
  750.         G = L + cr_g + cb_g;
  751.         B = L + cb_b;
  752.  
  753.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  754.         row1[0] = t;
  755.         row2[0] = t;
  756.         row1 += ONE_TWO;
  757.         row2 += ONE_TWO;
  758.  
  759.         /*
  760.          * Now, do second row.
  761.          */
  762. /* INTERPOLATE is now standard */
  763. #ifdef INTERPOLATE
  764.         if(y != rows - 2) 
  765.         {
  766.           CR = (unsigned int) (CR + *(cr + cols_2 - 1)) >> 1;
  767.           CB = (unsigned int) (CB + *(cb + cols_2 - 1)) >> 1;
  768.           cr_r = Cr_r_tab[CR];
  769.           cr_g = Cr_g_tab[CR];
  770.           cb_g = Cb_g_tab[CB];
  771.           cb_b = Cb_b_tab[CB];
  772.         }
  773. #endif
  774. /* endif */
  775.         L = L_tab[ (int) *lum2++];
  776.         R = L + cr_r;
  777.         G = L + cr_g + cb_g;
  778.         B = L + cb_b;
  779.  
  780.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  781.         row3[0] = t;
  782.         row4[0] = t;
  783.         row3 += ONE_TWO;
  784.         row4 += ONE_TWO;
  785.  
  786.         L = L_tab[(int) *lum2++];
  787.         R = L + cr_r;
  788.         G = L + cr_g + cb_g;
  789.         B = L + cb_b;
  790.  
  791.         t = (r_2_pix[R] | g_2_pix[G] | b_2_pix[B]);
  792.         row3[0] = t;
  793.         row4[0] = t;
  794.         row3 += ONE_TWO;
  795.         row4 += ONE_TWO;
  796.       }
  797.  
  798.       lum += cols_2 + cols_2;
  799.       lum2 += cols_2 + cols_2;
  800.  
  801.       row1 += ONE_TWO * 6 *cols_2;
  802.       row3 += ONE_TWO * 6 *cols_2;
  803.       row2 += ONE_TWO * 6 *cols_2;
  804.       row4 += ONE_TWO * 6 *cols_2;
  805.     }
  806. }
  807.  
  808. #endif /* COMMENTED_OUT */
  809.  
  810.  
  811.